/**
 * 回访详情  初始化
 */
var RevisitRecord = {
    id: "RevisitRecordTable",  //表格id
    seItem: null,    //选中的条目
    table: null,
    layerIndex: -1
};

var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
}
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
}

/**
 * 初始化表格的列
 */
RevisitRecord.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '', field: 'id', visible: false, align: 'center', valign: 'middle',},
        {
            title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<a href='javascript:;' onclick='RevisitRecord.reservationsArrives(" + '"' + row.id + '"' + ")'>" + row.customerName + "</a>";
                } else {
                    if (row.oneChannel == "电销部") {
                        return "<a href='javascript:;' onclick='RevisitRecord.reservationsNotDetails(" + '"' + row.id + '"' + ")'>" + row.customerName + "</a>"
                    } else {
                        return "<a href='javascript:;' onclick='RevisitRecord.reservationsNotDetail(" + '"' + row.id + '"' + ")'>" + row.customerName + "</a>"
                    }
                }
            },
        },
        {title: '手机号码', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true},
        {title: '到店状态', field: 'status', align: 'center', valign: 'middle', sortable: true,},
        {title: '回访状态', field: 'statuss', align: 'center', valign: 'middle', sortable: true,},
        {
            title: '一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '二级渠道', visible: false, field: 'twoChannel', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '三级渠道', visible: false, field: 'thrChannel', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                if (row.status == "到店") {
                    return "<span style='color: green'>" + value + "</span>"
                } else {
                    return value;
                }
            }
        },
        {
            title: '性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "男";
                } else if (value == 2) {
                    return "女"
                } else {
                    return "未知"
                }
            }
        },
        {title: '年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true},
        {
            title: '回访内容', field: 'huifang', align: 'center', valign: 'middle', sortable: true,width:450,
            formatter: function (value, row, index) {
                var reg = /,/g;
                return value.replace(reg, '');
            }
        },
        {title: '跟进人', field: 'follower', align: 'center', valign: 'middle', sortable: true},
        {title: '建档人', field: 'archiving', align: 'center', valign: 'middle', sortable: true},
        {title: '建档时间', field: 'filingTime', align: 'center', valign: 'middle', sortable: true},
        {title: '预约时间', field: 'appointmentTime', align: 'center', valign: 'middle', sortable: true},
    ]
    return columns;
};

/**
 * 查询 回访详情列表
 */
RevisitRecord.search = function () {
    RevisitRecord.table.refresh({query: RevisitRecord.formParams()});
};

RevisitRecord.reservationsArrives = function (id) {
    var index = layer.open({
        type: 2,
        title: '预约客户详情',
        area: ['100%', '100%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservationsArrive/reservations_arrives/' + id
    });
    this.layerIndex = index;
};

RevisitRecord.reservationsNotDetails = function (id) {
    var index = layer.open({
        type: 2,
        title: '预约客户详情',
        area: ['95%', '95%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservationsNotArrive/to_reservationsNotDetails/' + id
    });
    this.layerIndex = index;
};

RevisitRecord.reservationsNotDetail = function (id) {
    var index = layer.open({
        type: 2,
        title: '预约客户详情',
        area: ['95%', '95%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservationsNotArrive/to_reservationsNotDetail/' + id
    });
    this.layerIndex = index;
};

/**
 * 统计总人数
 */
RevisitRecord.huifangTongJi = function () {
    var param1 = $("#startRevisitTime").val();  //开始到店时间
    var param2 = $("#follower").val();  //跟进人
    var param3 = $("#startTime").val();
    var param4 = $("#endTime").val();
    $.ajax({
        type: "POST",
        url: Feng.ctxPath + "/reservationRevisit/totalNum",
        data: {
            param1: param1,
            param2: param2,
            param3: param3,
            param4: param4
        },
        success: function (data) {
            $("#huifangNum").html(data[0].countNum);//总人数
        }
    });
}


RevisitRecord.getFollower = function () {
    var index = layer.open({
        type: 2,
        title: '添加跟进人',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/reservations/to_follower'
    });
    this.layerIndex = index;
};

/**
 * 重置
 */
RevisitRecord.resetSearch = function () {
    $("#startRevisitTime").val(""); //开始时间
    $("#follower").val(""); //开始时间
    $("#startTime").val(""); //开始时间
    $("#endTime").val(""); //开始时间v
    document.getElementById("startRevisitTime").value = myDate.getFullYear() + "-" + month + "-" + day + "";
    RevisitRecord.search();
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
RevisitRecord.formParams = function () {
    var queryData = {};
    queryData['startRevisitTime'] = $("#startRevisitTime").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['startFilingTime'] = $("#startFilingTime").val();
    queryData['endFilingTime'] = $("#endFilingTime").val();
    queryData['follower'] = $("#follower").val();
    queryData['department'] = $("#department").val();
    return queryData;
}

$("#next").click(function () {
    var stime = document.getElementById("startRevisitTime").value;
    console.log(stime.length);
    var date = new Date(stime);
    var monthe;
    var daye;
    if (stime.length == 7) {
        date.setMonth(date.getMonth() + 1);
        if ((date.getMonth() + 1) < 10) {
            monthe = "0" + (date.getMonth() + 1);
        } else {
            monthe = "" + (date.getMonth() + 1);
        }

        document.getElementById("startRevisitTime").value = date.getFullYear() + "-" + monthe;
    }
    if (stime.length == 10) {
        date.setDate(date.getDate() + 1);
        if ((date.getMonth() + 1) < 10) {
            monthe = "0" + (date.getMonth() + 1);
        } else {
            monthe = "" + (date.getMonth() + 1);
        }
        if (date.getDate() < 10) {
            daye = "0" + date.getDate();
        } else {
            daye = "" + date.getDate();
        }
        //// 加一天
        document.getElementById("startRevisitTime").value = date.getFullYear() + "-" + monthe + "-" + daye;
    }
})

//把对象拼接成URL的键值对参数param1=value1&param2=value2
var parseParam = function (param) {
    var paramStr = "";

    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};

var getSingleData = function (param, key) {
    return key + "=" + param[key];
}

RevisitRecord.downLoad = function () {
    var queryData = {};
    queryData['startRevisitTime'] = $("#startRevisitTime").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['startFilingTime'] = $("#startFilingTime").val();
    queryData['endFilingTime'] = $("#endFilingTime").val();
    queryData['follower'] = $("#follower").val();
    var param = parseParam(queryData);
    location.href = Feng.ctxPath + '/reservationRevisit/reservationRevisitExport?' + param;
}

$("#minus").click(function () {
    var stime = document.getElementById("startRevisitTime").value;
    var date = new Date(stime);
    var monthe;
    var daye;
    if (stime.length == 7) {
        date.setMonth(date.getMonth() - 1);
        if ((date.getMonth() + 1) < 10) {
            monthe = "0" + (date.getMonth() + 1);
        } else {
            monthe = "" + (date.getMonth() + 1);
        }

        document.getElementById("startRevisitTime").value = date.getFullYear() + "-" + monthe;
    }
    if (stime.length == 10) {
        date.setDate(date.getDate() - 1);
        if ((date.getMonth() + 1) < 10) {
            monthe = "0" + (date.getMonth() + 1);
        } else {
            monthe = "" + (date.getMonth() + 1);
        }
        if (date.getDate() < 10) {
            daye = "0" + date.getDate();
        } else {
            daye = "" + date.getDate();
        }
        //// 加一天
        document.getElementById("startRevisitTime").value = date.getFullYear() + "-" + monthe + "-" + daye;
    }
})

$(function () {
    document.getElementById("startRevisitTime").value = myDate.getFullYear() + "-" + month + "-" + day;
    var stime = document.getElementById("startRevisitTime").value;
    var mtime = stime.substring(0, 7)
    $(".btn2").click(function () {
        $("#startRevisitTime").val(mtime)
        $("#startRevisitTime").attr("disabled", true)
    })
    $(".btn3").click(function () {
        $("#startRevisitTime").val(stime)
        $("#startRevisitTime").attr("disabled", false)
    })

    var oldStartValue1 = $("#startTime").val();
    var oldEndtimeValue1 = $("#endTime").val();

    var oldStartFilingValue1 = $("#startFilingTime").val();
    var oldEndtimeFilingValue1 = $("#endFilingTime").val();

    var defaultColunms = RevisitRecord.initColumn();
    var table = new BSTable(RevisitRecord.id, "/reservationRevisit/list1", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(RevisitRecord.formParams());
    RevisitRecord.table = table.init();
    RevisitRecord.search();
    RevisitRecord.huifangTongJi();

    //采用定时器监听 时间内容是否变化
    var oldStartValue = $("#startRevisitTime").val();
    var oldFollowerValue = $("#follower").val();
    setInterval(
        function () {//定时执行方法内容
            if ($("#startTime").val() != oldStartValue1 || $("#endTime").val() != oldEndtimeValue1) {//如果时间发生变化，就触发查询事件
                RevisitRecord.search();
                RevisitRecord.huifangTongJi();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                oldStartValue1 = $("#startTime").val();
                oldEndtimeValue1 = $("#endTime").val();
            }
            if ($("#startFilingTime").val() != oldStartFilingValue1 || $("#endFilingTime").val() != oldEndtimeFilingValue1) {//如果时间发生变化，就触发查询事件
                RevisitRecord.search();
                RevisitRecord.huifangTongJi();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                oldStartFilingValue1 = $("#startFilingTime").val();
                oldEndtimeFilingValue1 = $("#endFilingTime").val();
            }
            if ($("#startRevisitTime").val() != oldStartValue) {//如果时间发生变化，就触发查询事件
                RevisitRecord.search();
                RevisitRecord.huifangTongJi();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                oldStartValue = $("#startRevisitTime").val();
            }
            if (oldFollowerValue != $("#follower").val()) {
                RevisitRecord.search();
                RevisitRecord.huifangTongJi();
                oldFollowerValue = $("#follower").val();//每次判断为修改后把最新的文本框的值给原有变量，为下一次循坏执行方法体里面的判断做准备
            }
        }, 500);
});